#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int deleteAndEarn(vector<int>& nums) {
        int n = nums.size();
        sort(nums.begin(), nums.end());
        vector<int> arr(nums[n - 1] + 1, 0);
        for (int i = 0;i < n;i++)
        {
            //cout<<nums[i]<<" "<<arr.size()<<endl;
            arr[nums[i]] += nums[i];
            //cout<<arr[i]<<" ";
        }
        n = arr.size();
        vector<int> Yes(n, 0);
        auto No = Yes;
        Yes[0] = arr[0];
        for (int i = 1;i < n;i++)
        {
            Yes[i] = No[i - 1] + arr[i];
            No[i] = max(Yes[i - 1], No[i - 1]);
        }
        return max(Yes[n - 1], No[n - 1]);
    }
};